package org.rendao.service
{
	import com.googlecode.flexxb.FlexXBEngine;
	
	import org.rendao.entity.FirstIndex;
	import org.rendao.entity.SearchResult;
	import org.rendao.service.base.ServiceUtils;
	import org.rendao.utils.StringUtils;

	public class FirstIndexService
	{
		private static var _instance:FirstIndexService;
		
		public static function get instance():FirstIndexService {
			if (!_instance) {
				_instance=new FirstIndexService(new SingletonForcer());
			}
			return _instance;
		}
		
		public function FirstIndexService(sf:SingletonForcer) {
		}
		
		private static const GET_ALL_URL:String = "service/first_index/get_all";
		public function getAll(callback:Function):void {
			var onGet:Function = function(result:Object):void {
				var resultXml:XML = result as XML;
				var searchResult:SearchResult = FlexXBEngine.instance.deserialize(resultXml, SearchResult);
				callback(searchResult.records);
			};
			ServiceUtils.callService(GET_ALL_URL, onGet);
		}
		
		private static const GET_URL:String = "service/first_index/get/{id}";
		public function getById(id:String, callback:Function):void {
			var onGet:Function = function(result:Object):void {
				var resultXml:XML = result as XML;
				var firstIndex:FirstIndex = FlexXBEngine.instance.deserialize(resultXml, FirstIndex);
				callback(firstIndex);
			};
			var url:String = GET_URL.replace("{id}", id);
			ServiceUtils.callService(url, onGet);
		}
		
		private static const SAVE_URL:String = "service/first_index/save";
		public function save(firstIndex:FirstIndex, callback:Function):void {
			var onSave:Function = function(result:Object):void {
				var resultXml:XML = result as XML;
				var firstIndex:FirstIndex = FlexXBEngine.instance.deserialize(resultXml, FirstIndex);
				callback(firstIndex);
			};
			var content:XML = FlexXBEngine.instance.serialize(firstIndex);
			ServiceUtils.callService(SAVE_URL, onSave, content);
		}
		
		private static const DELETE_URL:String = "service/first_index/delete/{id}";
		public function remove(id:String, callback:Function):void {
			var onDelete:Function = function(result:Object):void {
				callback();
			};
			if(StringUtils.isNullOrEmpty(id)) {
				return;
			}
			var url:String = DELETE_URL.replace("{id}", id);
			ServiceUtils.callService(url, onDelete);
		}
	}
}
class SingletonForcer
{
}